package com.health.viewscontrols;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.health.R;

/**
 * 初始化配置
 *
 * @author nova
 *
 */
public class InitActivity extends NovaActivity implements OnClickListener {

	private EditText etProductName;
	private TextView tvResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.init_wsdl);
		Button btnSearch = (Button) findViewById(R.id.btnSearch);
		btnSearch.setOnClickListener(this);
		etProductName = (EditText) findViewById(R.id.etProductName);
		tvResult = (TextView) findViewById(R.id.tvResult);

	}

	@Override
	public void onClick(View v) {
		new WSAsyncTask().execute();

	}

	@Override
	public void findViews() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setListeners() {
		// TODO Auto-generated method stub

	}

	class WSAsyncTask extends AsyncTask {
		String str_result = "";

		@Override
		protected Object doInBackground(Object... params) {
			str_result = connectWs();
			// 必须使用post方法更新UI组件
			tvResult.post(new Runnable() {
				@Override
				public void run() {
					tvResult.setText(str_result);

				}
			});
			return null;
		}

	}

	private String connectWs() {
		String str_result = "";
		try {
			final String NAMESPACE = "http://webservice.yide.com/";
			final String URL = "http://192.168.88.65:8086/ehr/webservice/getCardIdCreater?wsdl";
			final String SOAP_ACTION = "http://webservice.yide.com/getCardIdCreater";
			final String METHOD_NAME = "getCardIdCreater";

			// 创建HttpTransportSE传输对象
			HttpTransportSE ht = new HttpTransportSE(URL);
			ht.debug = true;
			// 使用SOAP1.1协议创建Envelope对象
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			// 实例化SoapObject对象
			SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
			// 设置参数，参数名不一定需要跟调用的服务器端的参数名相同，只需要对应的顺序相同即可
			soapObject.addProperty("createorg", "411725100001");
			soapObject.addProperty("beginDate", "2016-1-1");
			soapObject.addProperty("endDate", "2016-1-20");
//			soapObject.addProperty("cardid", "411725100001");
			soapObject.addProperty("deviceId", "654321");
			// 将soapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
//			envelope.bodyOut = soapObject;
			envelope.setOutputSoapObject(soapObject);
			// 设置与.Net提供的Web Service保持较好的兼容性
			envelope.dotNet = true;


			try {
				// 调用Web Service
				ht.call(SOAP_ACTION, envelope);
				if (envelope.getResponse() != null) {
//					 SoapObject result = (SoapObject) envelope.bodyIn;
//					 // SoapObject detail = (SoapObject)
//					 // result.getProperty("out");
//					 // 用Object替代SoapObject以避免出现异常
//					 Object detail = (Object) result.getProperty(0);
//					 str_result = detail.toString();
//					 Log.e("et1Txt", detail.toString());
					SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
					//Assign it to Sum static variable
					str_result = response.toString();

					Log.e("eeeeeeeeeeeeeee", str_result.toString());
					// 解析服务器响应的SOAP消息
					return str_result.toString();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			str_result = "调用WebService错误.";
		}

		return str_result;
	}
}